const { expect, use } = require('chai');
const { BigNumber } = require('ethers');
const { deployContract, MockProvider, solidity } = require('ethereum-waffle');
const { ethers, upgrades } = require("hardhat");

use(solidity);

describe("Test LpToken Interface", function () {
    var ERC20;
    var OWNER;
    var ADDR1;
    var ADDR2;

    var LpToken;
    var StruggleDog;

    beforeEach(async () => {
        [OWNER, ADDR1, ADDR2] = await ethers.getSigners(); // 初始化地址参数
        const SRtotalSupply = 2000;
        const SSRTotalSupply = 3000;
        const URTotalSupply = 5000;

        const StruggleDogPauser = await ethers.getContractFactory("StruggleDog", OWNER);
        StruggleDog = await StruggleDogPauser.deploy(SRtotalSupply,SSRTotalSupply,URTotalSupply,"testDog", "Dog", "https://t721");

        const ERC20PresetMinterPauser = await ethers.getContractFactory("ERC20PresetMinterPauser", OWNER);
        ERC20 = await ERC20PresetMinterPauser.deploy("TestERC20", "T20");  // 初始化ERC20

        const LpTokenConstract = await ethers.getContractFactory("LpToken", OWNER);
        LpToken = await upgrades.deployProxy(LpTokenConstract, [ERC20.address, StruggleDog.address, OWNER.address, [1,2,3],[2,3,4]], {
            initializer: '__LpToken_init'
        });

        await LpToken.deployed();


        var mintERC721Tx1 = await StruggleDog.connect(OWNER).mint(OWNER.address, 1, 2);
        await mintERC721Tx1.wait();

        var mintERC721Tx2 = await StruggleDog.connect(OWNER).mint(ADDR1.address, 2, 3);
        await mintERC721Tx2.wait();

        var mintERC721Tx3 = await StruggleDog.connect(OWNER).mint(ADDR2.address, 3, 1);
        await mintERC721Tx3.wait();


        var approveERC721Tx1 = await StruggleDog.connect(OWNER).setApprovalForAll(LpToken.address, true);
        await approveERC721Tx1.wait();

        var approveERC721Tx2 = await StruggleDog.connect(ADDR1).setApprovalForAll(LpToken.address, true);
        await approveERC721Tx2.wait();

        var approveERC721Tx3 = await StruggleDog.connect(ADDR2).setApprovalForAll(LpToken.address, true);
        await approveERC721Tx3.wait();
    });

    it ("should be query income", async function () {
        var queryIncome3 = await LpToken.connect(ADDR2).queryIncome(); // ADDR2 的收益
    });

    it ("should be Pledge Accurate return", async function () {

        var mint1 = await LpToken.connect(OWNER).pledgeNFT([1]); //OWNER 质押3个
        await mint1.wait();

        var mintERC1 = await ERC20.connect(OWNER).mint(LpToken.address, BigNumber.from("10000000000000000000")); //  分红池进账10个U
        await mintERC1.wait();

        var fundsIncrease1 = await LpToken.connect(OWNER).fundsIncrease(BigNumber.from("10000000000000000000"), ERC20.address); // 分红池进账10个U
        await fundsIncrease1.wait();

        var mint2 = await LpToken.connect(ADDR1).pledgeNFT([2]); // ADDR1 质押4个
        await mint2.wait();
        var withdrawal = await LpToken.connect(OWNER).withdrawal(BigNumber.from("5000000000000000000")); // OWNER 提取5个U的收益;
        await withdrawal.wait();

        var mintERC2 = await ERC20.connect(OWNER).mint(LpToken.address, BigNumber.from("5000000000000000000")); //  分红池进账5个U
        await mintERC2.wait();

        var fundsIncrease2 = await LpToken.connect(OWNER).fundsIncrease(BigNumber.from("5000000000000000000"), ERC20.address); // 分红池进账 5U
        await fundsIncrease2.wait();

        var mint4 =  await LpToken.connect(ADDR2).pledgeNFT([3]); // ADDR2 质押2个
        await mint4.wait();

        var mintERC3 = await ERC20.connect(OWNER).mint(LpToken.address, BigNumber.from("5000000000000000000")); //  分红池进账 5U
        await mintERC3.wait();

        var fundsIncrease3 = await LpToken.connect(OWNER).fundsIncrease(BigNumber.from("5000000000000000000"), ERC20.address); // 模拟资金池进账5U
        await fundsIncrease3.wait();

        var burn1 =  await LpToken.connect(ADDR1).extractNFT([2]); // ADDR1 提取份额 3个
        await burn1.wait();

        var mintERC4 = await ERC20.connect(OWNER).mint(LpToken.address, BigNumber.from("10000000000000000000")); //  分红池进账 10U
        await mintERC4.wait();

        var fundsIncrease4 = await LpToken.connect(OWNER).fundsIncrease(BigNumber.from("10000000000000000000"), ERC20.address); // 模拟资金池进账10U
        await fundsIncrease4.wait();

        var burn2 =  await LpToken.connect(OWNER).extractNFT([1]); // ADDR1 提取份额 3个
        await burn2.wait();

        var mintERC5 = await ERC20.connect(OWNER).mint(LpToken.address, BigNumber.from("10000000000000000000")); //  分红池进账 10U
        await mintERC5.wait();

        var fundsIncrease5 = await LpToken.connect(OWNER).fundsIncrease(BigNumber.from("10000000000000000000"), ERC20.address); // 模拟资金池进账10U
        await fundsIncrease5.wait();
        var queryIncome1 = await LpToken.connect(OWNER).queryIncome(); // Owner 的收益
        var queryIncome2 = await LpToken.connect(ADDR1).queryIncome(); // ADDR1 的收益

        var queryIncome3 = await LpToken.connect(ADDR2).queryIncome(); // ADDR2 的收益

    });
});


